/*******************************************************************************
Jimm - Mobile Messaging - J2ME ICQ clone
Copyright (C) 2003-04  Jimm Project

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 ********************************************************************************
File: src/jimm/comm/SysNoticeAction.java
Version: ###VERSION###  Date: ###DATE###
Author(s): Andreas Rossbacher
 *******************************************************************************/
// #sijapp cond.if protocols_ICQ is "true" #
package protocol.icq.action;

import jimm.*;
import jimm.chat.message.SystemNotice;
import jimm.comm.*;
import protocol.icq.packet.*;

public class SysNoticeAction extends IcqAction {

    // System notice
    private SystemNotice notice;

    // Constructor
    public SysNoticeAction(SystemNotice _notice) {
        super();
        this.notice = _notice;
    }

    // Init action
    public void init() throws JimmException {

        byte[] buf;

        //	Get byte Arrys from the stuff we need the length of
        byte[] uinRaw = StringConvertor.stringToByteArray(this.notice.getSndrUin());
        byte[] reasonRaw = StringConvertor.stringToByteArray(this.notice.getReason());

        // If system notice is a SYS_NOTICE_AUTHORISE
        if (notice.getSysnoteType() == SystemNotice.SYS_NOTICE_AUTHORISE) {

            Util stream = new Util();
            stream.writeShortLenAndUtf8String(notice.getSndrUin());
            stream.writeByte(notice.isAUTH_granted() ? 0x01 : 0x00);
            stream.writeLenAndUtf8String(notice.getReason());
            // Send a CLI_AUTHORIZE packet
            sendPacket(new SnacPacket(SnacPacket.SSI_FAMILY,
                    SnacPacket.CLI_AUTHORIZE_COMMAND, 0x0000001A, stream.toByteArray()));

            //	If system notice is a SYS_NOTICE_REQUAUTH
        } else if ((notice.getSysnoteType() == SystemNotice.SYS_NOTICE_REQUAUTH)
                || (notice.getSysnoteType() == SystemNotice.SYS_NOTICE_GRAND)) {


            Util stream = new Util();
            stream.writeShortLenAndUtf8String(notice.getSndrUin());
            stream.writeLenAndUtf8String(notice.getReason());
            stream.writeWordBE(0x0000);
            int cmd = (notice.getSysnoteType() == SystemNotice.SYS_NOTICE_REQUAUTH)
                    ? SnacPacket.CLI_REQAUTH_COMMAND
                    : SnacPacket.CLI_GRANT_FUTURE_AUTH_COMMAND;
            // Send a CLI_REQUAUTH packet
            sendPacket(new SnacPacket(SnacPacket.SSI_FAMILY, cmd,
                    0x00000018, stream.toByteArray()));
        }

    }

    // Forwards received packet, returns true if packet was consumed
    public boolean forward(Packet packet) throws JimmException {
        return false;
    }

    // Returns true if the action is completed
    public boolean isCompleted() {
        return true;
    }

    // Returns true if an error has occured
    public boolean isError() {
        return false;
    }
}
// #sijapp cond.end #
